package android.FiltrosJava;

import java.awt.image.BufferedImage;

public class CropImage extends AbstractImageBasic{
	int janela = 20;
	float percL, percR, percT, percD;
	public CropImage(float percLeft, float percRight, float percTop, float percDown){
		setCrop(percLeft, percRight, percTop, percDown);
	}

	public void setCrop(float percLeft, float percRight, float percTop, float percDown){
		percL = verificaValor(percLeft);
		percR = verificaValor(percRight);
		percT = verificaValor(percTop);
		percD = verificaValor(percDown);		
	}
	
	private float verificaValor(float v){
		if (v < 0.0f)
			return 0.0f;
		else
			if (v > 0.95f)
				return 0.95f;
		
		return v;	
	}
	
	public BufferedImage applyFilter(BufferedImage src) {
		int x = (int) (percL * src.getWidth() / 2.0f);		
		int y = (int) (percT * src.getHeight() / 2.0f);
		int w = (int) (percR * src.getWidth() / 2.0f);
		int h = (int) (percD * src.getHeight() / 2.0f);
		
		w = src.getWidth() - w - x;
		h = src.getHeight() - h - y;
			
        return src.getSubimage(x, y, w, h);
    }
}